/*
 * Copyright 2009 Gary Brown
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *
 * Change History:
 * 5 April 2009 : Initial version created by gary
 */
package org.betonthemove.ui.android;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class EventTypeSelection extends Activity
					implements OnItemClickListener, OnClickListener {

	public static final String EVENT_TYPE_ID_ATTR = "org.betonthemove.EventTypeID";

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.eventtypeselection);
        
        m_grid = (GridView) findViewById(R.id.eventtypegrid);
        m_grid.setAdapter(new ImageAdapter(this));
        
        m_grid.setOnItemClickListener(this);
        
        Button b=(Button)findViewById(R.id.show_all_event_types);
        b.setOnClickListener(this);
    }
    
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		if (Log.isLoggable("EventTypeSelection", Log.DEBUG)) {
			Log.d("EventTypeSelection", "Item clicked arg0="+arg0+
					" arg1="+arg1+" arg2="+arg2+" arg3="+arg3);
		}
		
        Intent intent = new Intent();
        intent.setClass(EventTypeSelection.this, MarketSelection.class);
        
        int id=m_eventTypeIds[arg2];
        
		if (Log.isLoggable("EventTypeSelection", Log.DEBUG)) {
			Log.d("EventTypeSelection", "ID="+id);
		}
        
        intent.putExtra(EVENT_TYPE_ID_ATTR, id);
        
        startActivity(intent);
	}

	public void onClick(View arg0) {
        Intent intent = new Intent();
        intent.setClass(EventTypeSelection.this, FullEventTypeSelection.class);
        startActivity(intent);
	}
	
	/* Creates the menu items */
	public boolean onCreateOptionsMenu(Menu menu) {
	    return(OptionsMenuSupport.onCreateOptionsMenu(menu));
	}

	/* Handles item selections */
	public boolean onOptionsItemSelected(MenuItem item) {
	    return(OptionsMenuSupport.onOptionsItemSelected(EventTypeSelection.this, item));
	}

	protected void onDestroy() {
		super.onDestroy();
		
		Intent intent=new Intent();
		intent.setClass(EventTypeSelection.this, SignOut.class);
		startActivity(intent);
	}

    private GridView m_grid;

    private Integer[] m_eventTypeImages = {
    		R.drawable.american_football,
    		R.drawable.baseball,
    		R.drawable.basketball,
    		R.drawable.boxing,
    		R.drawable.golf,
    		R.drawable.horseracing,
    		R.drawable.icehockey,
    		R.drawable.soccer,
    		R.drawable.tennis,
    		R.drawable.volleyball
    };

    private Integer[] m_eventTypeIds = {
    	    6423,
    	    7511,
    	    7522,
    	    6,
    	    3,
    	    7,
    	    7524,
    	    1,
    	    2,
    	    998917
    };

    private Integer[] m_eventTypeNames = {
    		R.string.football_eventtypename,
    		R.string.baseball_eventtypename,
    		R.string.basketball_eventtypename,
    		R.string.boxing_eventtypename,
    		R.string.golf_eventtypename,
    		R.string.horseracing_eventtypename,
    		R.string.icehockey_eventtypename,
    		R.string.soccer_eventtypename,
    		R.string.tennis_eventtypename,
    		R.string.volleyball_eventtypename
    };

	public class ImageAdapter extends BaseAdapter {
        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return m_eventTypeImages.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
        	/*
            ImageView imageView;
            if (convertView == null) {
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(64, 64));
                imageView.setAdjustViewBounds(false);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
            } else {
                imageView = (ImageView) convertView;
            }

            imageView.setImageResource(m_eventTypeImages[position]);

            return imageView;
            */
        	Drawable image=getResources().getDrawable(m_eventTypeImages[position]);
        	String text=getResources().getString(m_eventTypeNames[position]);
        	
        	TextView ret=new TextView(mContext);
        	ret.setText(text);
        	ret.setGravity(Gravity.CENTER_HORIZONTAL);
        	ret.setCompoundDrawablesWithIntrinsicBounds(null, image, null, null);
        	
        	return(ret);
        }

        private Context mContext;
    }
}